package com.situ.web.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.situ.web.entity.Admin;
import com.situ.web.mapper.ILoginMapper;
import com.situ.web.service.ILoginService;
import com.situ.web.util.MyBatisUtil;

public class LoginServiceImpl implements ILoginService {
	
	private ILoginMapper loginMapper;
	
	@Override
	public Admin login(String name, String password) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		loginMapper = sqlSession.getMapper(ILoginMapper.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name",name);
		map.put("password",password);
		Admin admin = loginMapper.login(map);
		return admin;
	}



	@Override
	public boolean register(String name, String email,String password) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		loginMapper = sqlSession.getMapper(ILoginMapper.class);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name",name);
		map.put("email",email);
		map.put("password",password);
		int count = loginMapper.register(map);
		sqlSession.commit();
		sqlSession.close();
		return count == 1 ? true : false;
	}

	@Override
	public Admin selectByName(String name) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		loginMapper = sqlSession.getMapper(ILoginMapper.class);
		return loginMapper.selectByName(name);
	}



	@Override
	public Admin selectByEmail(String email) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		loginMapper = sqlSession.getMapper(ILoginMapper.class);
		return loginMapper.selectByEmail(email);
	}

}
